<template>
  <div>
      <input type="text" class="classify_inp" placeholder="回车搜索商品">
      <div class="classify_container">
          <div class="classify_items_left">
              <p 
              :class="isActive == item.id?'classify_left_item_active':'classify_left_item'" 
              v-for="(item, index) in classify" 
              :key="index"
              @click="changeActive(item)"
              >{{item.name}}</p>
          </div>
          <div class="classify_items_right">
            <div 
            class="classify_right_item" 
            v-for="(item, index) in goods" 
            :key="index"
            @click="goGoodsList(item)">
                <div class="classiay_item_icon">
                    <img :src="item.icon" width="100%">
                </div>
                <div class="clasify_item_text">{{item.name}}</div>
            </div>
        </div>
      </div>
  </div>
</template>

<script>
export default {
    data() {
        return {
            isActive: 0,
            allData: [],
            classify: [],
            goods: []
        }
    },
    mounted() {
        this.$API.getClassify().then(res=> {
            // console.log(res.data.data);
            this.allData = res.data.data;
            this.classify.push({name: '全部商品', id: 0})
            this.allData.forEach(item=> {
                if(item.pid == 0) {
                    this.classify.push(item)
                }else {
                    this.goods.push(item)
                }
            })
        })
    },
    methods: {
        changeActive(item) {
            this.goods = []
            this.isActive = item.id;
            this.allData.forEach(data=> {
                if(item.name == '全部商品') {
                    if(data.pid != 0) {
                        this.goods.push(data)
                    }
                }else if(item.id == data.pid) {
                    this.goods.push(data)
                }
            })
        },
        goGoodsList(item) {
            this.$router.push({
                path: '/goodsList',
                query: {
                    item
                }
            })
        }
    }
}
</script>

<style scoped>
.classify_inp {
    width: 60%;
    height: 0.4rem;
    margin: 0.1rem 0;
    margin-left: 0.1rem;
    border-radius: 0.4rem;
    background-color: #eee;
    border: none;
    outline: none;
    font-size: 0.2rem;
    text-indent: 0.2rem;
}
.classify_container {
    width: 100%;
    display: flex;
}
.classify_items_left {
    flex: 4;
    height: 13.6rem;
    border-top: 1px solid #eee;
    border-right: 1px solid #eee;
    overflow-y: scroll;
    text-align: center;
    font-size: 0.3rem;
    color: #666;
}
.classify_items_left::-webkit-scrollbar {
    display: none;
}
.classify_left_item_active {
    color: red !important;
    border-left: 0.05rem solid red;
}
.classify_items_right {
    flex: 12;
    display: flex;
    justify-content: flex-start;
    align-content: flex-start;
    flex-wrap: wrap;
    height: 13.6rem;
    border-top: 1px solid #eee;
    overflow: scroll;
}
.classify_items_right::-webkit-scrollbar {
    display: none;
}
.classify_right_item {
    width: 30%;
    height: 2rem;
    text-align: center;
    font-size: 0.2rem;
    color: #666;
}
.classiay_item_icon {
    width: 90%;
    height: 1.5rem;
    margin: 5%;
}
</style>